/*********************************************-*- mode:c -*-
 * board_avrnetio.h
 * this file contains all board specific definitions
 * not the cpu spcific ones!
 *
 *  Part of AVR-Netino - http://code.google.com/p/avr-netino/
 *
 * Copyright (c) 2011 Michael Maassen
 * 
 * 2011-01-04:	port to AVR-Net-IO by M.Maassen <mic.maassen@gmail.com>
 * 2011-01-23:	add A0..A3 for better compatiblity with arduino
 * 2012-01-10:  port to Arduino IDE Ver. 1.0
 * 2015-02-14:  port to Arduino 1.0.6/1.6.0
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version, or
 * 
 * under the terms of the  Common Development and Distribution License (CDDL) 
 * version 1, see http://www.opensource.org/licenses/cddl1.php
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * $Id$
 *
 ************************************************************
 * you must define one of the folowing macros to 
 * use this file:
 *
 * Names for special pins or hardware constants:
 *   -  as const static:
 *      #define defConstant(N,V) const static uint8_t N = V;
 *   -  as enum element, inside: enum xxx { ... };
 *      #define defConstant(N,V) N = V,
 * defConstants		get all pin fuctions by names
 *
 * pinDef(p,b,t,f,u,i,...)	define a pin by port,bit,timer,function,usage,pcint...
 *   - all in 1 byte (for m168/m328/m32/m644(p)
 *     #define pinDef(p,b,t,...)	((((P##p)-1)<<5) | (b) | ((t)<<3))
 *   - all in 2 byte (for m1280/m2560
 *     #define pinDef(p,b,t,...)	((((P##p)-1)<<8) | (b) | ((t)<<3))
 *   - only port for digital_pin_to_port_PGM[]
 *     #define pinDef(p,...)	(P##p)
 *   - only bit mask for digital_pin_to_bit_mask_PGM[]
 *     #define pinDef(p,b,...)	_BV(b)
 *   - only timer for digital_pin_to_timer_PGM[]
 *     #define pinDef(p,b,t,...)	(t)
 *   - all in struct pin_map_t { } for (SdFat/Sd2PinMap.h)
 *     #define pinDef(p,b,t,...)	{&DDR##p, &PIN##p, &PORT##p, b},
 *   - port name as string in a const char* pname[] = {...}
 *     #define pinDef(p,b,...) "P"#p#b
 *
 * anaDef(a,p,c,...)		define an analog input by A#,Pin,Chan
 *   - A# in an enum
 *     #define anaDef(a,p,c,...) A##a = (p),
 *   - map input to adc channel
 *     #define anaDef(a,p,c,...)	(c),
 *   - man analog to digital pins
 *     #define anaDef(a,p,c,...)	(p),
 *
 * Arduino Defines (optional):
 *   - ArduinoDefs_Digital
 *   - ArduinoDefs_Analog
 *   - ArduinoDefs_PWM
 *   - ArduinoDefs_PCINT
 *   - ArduinoDefs_USB
 *   - ArduinoDefs_Serial
 *   
 ***********************************************************
 * Board:	AVR-Net-IO	by Pollin.de
  * On the Arduino board, digital pins are also used
 * for the analog output (software PWM).  Analog input
 * pins are a separate set.

 * ATMEL ATMEGA32 & 644(P) / AVR-NetIO
 *
 *			      +---\/---+
 *            T0 (D 24)	PB0  1|        |40  PA0 (AI 0 / D8)
 *            T1 (D 25) PB1  2|        |39  PA1 (AI 1 / D9)
 *          INT2 (D 26) PB2  3|        |38  PA2 (AI 2 / D10)
 *           PWM (D 27) PB3  4|        |37  PA3 (AI 3 / D11)
 *       PWM+ SS (D 28) PB4  5|        |36  PA4 (AI 4 / D12)
 *          MOSI (D 29) PB5  6|        |35  PA5 (AI 5 / D13)
 *          MISO (D 30) PB6  7|        |34  PA6 (AI 6 / D14)
 *           SCK (D 31) PB7  8|        |33  PA7 (AI 7 / D15)
 *                      RST  9|        |32  AREF
 *                      VCC 10|        |31  GND 
 *                      GND 11|        |30  AVCC
 *                    XTAL2 12|        |29  PC7 (D  7)
 *                    XTAL1 13|        |28  PC6 (D  6)
 *           RX0 (D 16) PD0 14|        |27  PC5 (D  5) TDI
 *           TX0 (D 17) PD1 15|        |26  PC4 (D  4) TDO
 *      RX1 INT0 (D 18) PD2 16|        |25  PC3 (D  3) TMS
 *      TX1 INT1 (D 19) PD3 17|        |24  PC2 (D  2) TCK
 *           PWM (D 20) PD4 18|        |23  PC1 (D  1) SDA
 *           PWM (D 21) PD5 19|        |22  PC0 (D  0) SCL
 *           PWM+(D 22) PD6 20|        |21  PD7 (D 23) PWM
 *                            +--------+
 *
 * PWM+ 644 only
 *
 * D0 ..D11 are on SubD25
 * D12..D15 are on screw connectors
 * D16..D17 are RS232 
 * D18..D24,D26 are on EXT. connectors

 ***********************************************************/

/***********************************************************
 * things we want allways 
 ***********************************************************/
// we define AVR_NET_IO to flag different pinnig to libraries
// if set, we compile for Pollin AVR-Net-IO board
#ifndef AVR_NET_IO 
#define AVR_NET_IO	0x20150214	/* Date YYYMMDD */
#endif

#ifdef pinDef
/***********************************************************
 * mapping from pin number to :
 * Port,Bit,Timer(PWM),spec.function,usage,pcint,...
 ***********************************************************/
/* ATmega with 4 Ports (m32/m644) = AVR-Net-IO */
/* 0 */
pinDef( C, 0 , NOT_ON_TIMER,SCL,J3_2,16)
pinDef( C, 1 , NOT_ON_TIMER,SDA,J3_3,17)
pinDef( C, 2 , NOT_ON_TIMER,TCK,J3_4,18)
pinDef( C, 3 , NOT_ON_TIMER,TMS,J3_5,19)
pinDef( C, 4 , NOT_ON_TIMER,TDO,J3_6,20)
pinDef( C, 5 , NOT_ON_TIMER,TDI,J3_7,21)
pinDef( C, 6 , NOT_ON_TIMER,TOSC1,J3_8,22)
pinDef( C, 7 , NOT_ON_TIMER,TOSC2,J3_9,23)
/* 8 */
pinDef( A, 0 , NOT_ON_TIMER,ADC0,A0,0)
pinDef( A, 1 , NOT_ON_TIMER,ADC1,A1,1)
pinDef( A, 2 , NOT_ON_TIMER,ADC2,A2,2)
pinDef( A, 3 , NOT_ON_TIMER,ADC3,A3,3)
pinDef( A, 4 , NOT_ON_TIMER,ADC4,ADC_1,4)
pinDef( A, 5 , NOT_ON_TIMER,ADC5,ADC_2,5)
pinDef( A, 6 , NOT_ON_TIMER,ADC6,ADC_3,6)
pinDef( A, 7 , NOT_ON_TIMER,ADC7,ADC_4,7)
/* 16 */
pinDef( D, 0 , NOT_ON_TIMER,RXD,RS232_RxD,24)
pinDef( D, 1 , NOT_ON_TIMER,TXD,RS232_TxD,25)
pinDef( D, 2 , NOT_ON_TIMER,INT0,LED_1,26)
pinDef( D, 3 , NOT_ON_TIMER,INT1,RFM12_IRQ,27)
pinDef( D, 4 , TIMER1B,OC1B,LED_2,28)
pinDef( D, 5 , TIMER1A,OC1A,RFM12_CS,29)
#ifdef COM21
pinDef( D, 6 , NOT_ON_TIMER,ICP1,LED_3,30)
pinDef( D, 7 , TIMER2,OC2,SDcard_INS,31)
#else
pinDef( D, 6 , TIMER2B,OC2B,LED_3,30)
pinDef( D, 7 , TIMER2A,OC2A,SDcard_INS,31)
#endif
/* 24 */
pinDef( B, 0 , NOT_ON_TIMER,T0,IR_Rx,8)
pinDef( B, 1 , NOT_ON_TIMER,T1,JUMP_PROG,9)
pinDef( B, 2 , NOT_ON_TIMER,INT2,ENC28J60_IRQ,10)
pinDef( B, 3 , TIMER0A,OC0,SDcard_CS,11)
#ifdef COM0B1
pinDef( B, 4 , TIMER0B,SS,ENC28J60_CS,12)
#else
pinDef( B, 4 , NOT_ON_TIMER,SS,ENC28J60_CS,12)
#endif
pinDef( B, 5 , NOT_ON_TIMER,MOSI,SPI_MOSI,13)
pinDef( B, 6 , NOT_ON_TIMER,MISO,SPI_MISO,14)
pinDef( B, 7 , NOT_ON_TIMER,SCK,SPI_SCK,15)
#endif /* pinDef */

#ifdef anaDef
/***********************************************************
 * mapping from analog pin to:
 * A*,digPin,adcChannel,
 ***********************************************************/
anaDef( 0, 8, 0)
anaDef( 1, 9, 1)
anaDef( 2, 10, 2)
anaDef( 3, 11, 3)
anaDef( 4, 12, 4)
anaDef( 5, 13, 5)
anaDef( 6, 14, 6)
anaDef( 7, 15, 7)
#endif	/* anaDef */

/***********************************************************
 * the following ArduinoDefs_* defines are optional
 * if defined these can lead to faster/smaller code
 ***********************************************************/
#if defined( ArduinoDefs_Digital )
#endif	/* ArduinoDefs_Digital */

#if defined( ArduinoDefs_Analog )
#define analogPinToChannel(P)		(P)
#define analogInputToDigitalPin(p)	(((p) < pins_count_analog) ? (p) + pins_ADC0 : -1)
#endif	/* ArduinoDefs_Analog */

#if defined( ArduinoDefs_PWM )
#if defined(COM21)
#if defined(COM0B1)
#define digitalPinHasPWM(p)         ((p) == 20 || (p) == 21 || (p) == 23 || (p) == 27 || (p) == 28)
#else
#define digitalPinHasPWM(p)         ((p) == 20 || (p) == 21 || (p) == 23 || (p) == 27)
#endif	/* COM0B1 */
#else
#if defined(COM0B1)
#define digitalPinHasPWM(p)         ((p) == 20 || (p) == 21 || (p) == 22 || (p) == 23 || (p) == 27 || (p) == 28)
#else
#define digitalPinHasPWM(p)         ((p) == 20 || (p) == 21 || (p) == 22 || (p) == 23 || (p) == 27)
#endif	/* COM0B1 */
#endif	/* COM21 */

#endif	/* ArduinoDefs_PWM */

#if defined( ArduinoDefs_PCINT )
#ifdef PCICR
#define digitalPinToPCICR(p)    (((p) >= 0 && (p) <= 31) ? (&PCICR) : ((uint8_t *)0))
#define digitalPinToPCICRbit(p) (((p) <= 7) ? 2 : (((p) <= 15) ? 0 : (((p) <= 23) ? 3 : 1)))
#define digitalPinToPCMSK(p)    (((p) <= 7) ? (&PCMSK2) : (((p) <= 15) ? (&PCMSK0) : (((p) <= 23) ? (&PCMSK3) : (((p) <= 31) ? (&PCMSK1) :((uint8_t *)0)))))
#define digitalPinToPCMSKbit(p) ((p) & 7)
#else
/* no PCINT (mega32) */
#define digitalPinToPCICR(p)    ((uint8_t *)0)
#define digitalPinToPCICRbit(p) 0
#define digitalPinToPCMSK(p)    ((uint8_t *)0)
#define digitalPinToPCMSKbit(p) 0
#endif /* PCICR */
#endif	/* ArduinoDefs_PCINT */

#if defined( ArduinoDefs_USB )
#endif	/* ArduinoDefs_USB */

#if defined( ArduinoDefs_Serial )
// These serial port names are intended to allow libraries and architecture-neutral
// sketches to automatically default to the correct port name for a particular type
// of use.  For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
//
// SERIAL_PORT_MONITOR        Port which normally prints to the Arduino Serial Monitor
//
// SERIAL_PORT_USBVIRTUAL     Port which is USB virtual serial
//
// SERIAL_PORT_LINUXBRIDGE    Port which connects to a Linux system via Bridge library
//
// SERIAL_PORT_HARDWARE       Hardware serial port, physical RX & TX pins.
//
// SERIAL_PORT_HARDWARE_OPEN  Hardware serial ports which are open for use.  Their RX & TX
//                            pins are NOT connected to anything by default.

#define SERIAL_PORT_MONITOR         Serial
#define SERIAL_PORT_HARDWARE        Serial
#if defined (UBRR1H)
#define SERIAL_PORT_HARDWARE1       Serial1
#define SERIAL_PORT_HARDWARE_OPEN   Serial1
#endif

#endif /* ArduinoDefs_Serial */

#ifdef defConstant
/***********************************************************
 * define constants for special pins
 ***********************************************************/
/* define board specific pin functions */

#ifndef defClass
#define defClass(C,N) C##_##N
#endif 


#if defined( All_Constants ) || defined( Arduino_Constants )
defConstant(defClass(ARDUINO,LED_BUILTIN), pins_LED_1)
#endif

#if defined( All_Constants ) || defined( LCD_Constants )
// LCD in 4BitMode at PCF8574 
// IMPORTANT: Pin1&2 of the LCD connector are NOT usual at the AddOn board 
defConstant(defClass(I2C_LCD,ADR) , 0x20) //PCF8574=0x20 PCF8574A=0x38 (7Bit adr)
defConstant(defClass(I2C_LCD,NBL) , 0   )	// 4Bit-Bus at P0-P3 
defConstant(defClass(I2C_LCD,RS)  , 0x10)	// P4 D/I
defConstant(defClass(I2C_LCD,RW)  , 0x20)	// P5 R/W
defConstant(defClass(I2C_LCD,E)   , 0x40)	// P6 Enable
defConstant(defClass(I2C_LCD,BL)  , 0x80)	// P7 Back Light 
#endif

#if defined( All_Constants ) || defined( IRQ_Constants )
defConstant(defClass(ENC28J60,INR) ,  2  )	// Int Nr of ENC28J60
defConstant(defClass(RFM12,INR)	   ,  1  )	// Int Nr of RFM12
#endif

defConstant(defClass(_internal_,_avoid_empty_enum_),0)
#endif	/* defConstant */
/***********************************************************
 * clean up Xmacros
 ***********************************************************/
#undef pinDef
#undef anaDef
#undef defConstant
#undef defClass
#undef All_Constants
